package com.app.simpsonux;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.SharedPreferences;

public class RegistViewActivity extends Activity implements View.OnClickListener 
{
    int    mStepNum;
    
    String mUserName;
    String mUserId;
    String mUserPassword;
     
    private static final int WIZARD_PAGE_ONE = 1;
    private static final int WIZARD_PAGE_TWO = 2;
    private static final int WIZARD_PAGE_THREE = 3;
    
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.regiwizardpage1 );
        setButtonListener( WIZARD_PAGE_ONE );
        
        mStepNum      = WIZARD_PAGE_ONE;
        mUserName     ="";
        mUserId       ="";
        mUserPassword ="";
    }
    
    public void setButtonListener( int aPageNum )
    {
        if( aPageNum == WIZARD_PAGE_THREE )
        {
            ( ( Button )findViewById( R.id.buttonRegist ) ).setOnClickListener( this );
        }
        else
        {
            ( ( Button )findViewById( R.id.buttonNext ) ).setOnClickListener( this );
        }
        
        ( ( Button )findViewById( R.id.buttonBack ) ).setOnClickListener( this );
        ( ( Button )findViewById( R.id.buttonCancel ) ).setOnClickListener( this );
    }
    
    @Override
    public void onClick(View v)
    {
        switch( v.getId() )
        {
        case R.id.buttonNext:
            if( mStepNum < WIZARD_PAGE_THREE )
            {
                switch( mStepNum )
                {
                case WIZARD_PAGE_TWO:
                    mUserName = ( ( EditText )findViewById( R.id.editforusername ) ).getText().toString();
                    this.setContentView(R.layout.regiwizardpage3);
                    setButtonListener( ++mStepNum );
                    ( ( EditText ) findViewById( R.id.editforuserpassword ) ).setText( mUserPassword );
                    break;
                case WIZARD_PAGE_ONE:
                default:
                    mUserId = ( ( EditText )findViewById( R.id.editforuserid ) ).getText().toString();
                    
                    if( checkExistID( mUserId ) == true )
                    {
                        UXUt.AlertDlg(this, "User Regist", "Already registered user.", false );
                    }
                    else
                    {
                        this.setContentView(R.layout.regiwizardpage2);
                        setButtonListener( ++mStepNum );
                        ( ( EditText ) findViewById( R.id.editforusername ) ).setText( mUserName );
                    }
                    break;
                }
            }
            else
            {
                // Do nothing.
            }
            break;
        case R.id.buttonBack:
            if( mStepNum > WIZARD_PAGE_ONE )
            {
                switch( mStepNum )
                {
                case WIZARD_PAGE_THREE:
                    mUserPassword = ( ( EditText )findViewById( R.id.editforuserpassword ) ).getText().toString();
                    this.setContentView( R.layout.regiwizardpage2 );
                    setButtonListener( --mStepNum );
                    ( ( EditText )findViewById( R.id.editforusername ) ).setText( mUserName );
                    break;
                case WIZARD_PAGE_TWO:
                default:
                    mUserName = ( ( EditText )findViewById( R.id.editforusername ) ).getText().toString();
                    this.setContentView( R.layout.regiwizardpage1 );
                    setButtonListener( --mStepNum );
                    ( ( EditText )findViewById( R.id.editforuserid ) ).setText( mUserId );
                    break;
                }
            }
            else
            {
                finish();
            }
            break;
        case R.id.buttonRegist:
        	 mUserPassword = ( ( EditText )findViewById( R.id.editforuserpassword ) ).getText().toString();
            saveConf();
        case R.id.buttonCancel:
             finish();
            break;
        default:
            break;
        }
    }
    
    public boolean saveConf()
    {
        SharedPreferences sPreference = getSharedPreferences( mUserId, MODE_PRIVATE );
        
        SharedPreferences.Editor sEdit = sPreference.edit( );
        
        // User Name
        sEdit.putString(
                getString( R.string.pref_field_user_name ),
                mUserName );
        
        // User ID
        sEdit.putString(
                getString( R.string.pref_field_user_id ),
                mUserId );
        
        // User Password
        sEdit.putString(
                getString( R.string.pref_field_user_password ),
                mUserPassword );
        
        sEdit.commit();
        
        return true;
    }
    
    public boolean checkExistID( String aUserId )
    {
        String sUserId         = "";
        
        SharedPreferences sPreference = getSharedPreferences(
                                           aUserId,
                                           MODE_PRIVATE );
        if( sPreference == null )
        {
            return false;
        }
        else
        {
            sUserId = sPreference.getString( getString( R.string.pref_field_user_id ), "" );
            if( sUserId.compareTo( aUserId ) == 0 )
            {
                UXUt.AlertDlg(this, this.toString(), aUserId, false);
                UXUt.AlertDlg(this, this.toString(), sUserId, false);
                return true;
            }
            else
            {
                // do nothing.
            }
        }
        return false;
    }
};
